/************************************************************************************************
 * test examples of 100 interesting program in C
 * test 001.c
 * draw sine and cosine graph with "*"
 ***********************************************************************************************/

#include <stdio.h>
#include <math.h>

/*
 * draw graph with ASCII charactors, using horizon x axis. It's possible, but difficult bcz y is
 * not continuous when x is continous by charactor width (in common graph drawing, we also using
 * continuous x as default) and we need to jump between lines. so, in this drawing, we force y
 * to be continuous, and calculate x with inverse function.
 * since y is between -1 and 1, x is between 0 and 2*pi. but 2*pi is too small to draw a picture,
 * we use 10*2*pi, which is about 62.
 */

int main()
{
	double y = 0.0;
	int x = 0, m = 0;
        // cosine
	for (y = 1.0; y >= -1.0; y -= 0.1)
	{
		m = acos(y)*10;
		for (x = 0; x < m; x++) printf(" ");
		printf("*");
		for (; x < 62-m; x++) printf(" ");
		printf("*\n");
	}
        // sine
	for (y = 1.0; y >= 0; y -= 0.1)
	{
		m = asin(y)*10;
		for (x = 0; x < m; x++) printf(" ");
		printf("*");
		for (; x < 31-m; x++) printf(" ");
		printf("*\n");
	}
	for (; y >= -1.0; y -= 0.1)
	{
		m = asin(y)*10;
		for (x = 0; x < 31-m; x++) printf(" ");
		printf("*");
		for (; x < 62+m; x++) printf(" ");
		printf("*\n");
	}
}
